1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
%{
#include <stdint.h>
#include <stdio.h>
#include <bu/string.h>
typedef void *yyscan_t;
%}
%locations
%define api.pure
%debug
%error-verbose
%parse-param { yyscan_t yyscanner }
%lex-param { yysacn_t yyscanner }
%union {
int64_t iValue;
double dValue;
Bu::String *sValue;
bool bValue;
}
%{
int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner );
void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, const char *error )
{
printf("%d:%d-%d:%d: %s\n",
llocp->first_line, llocp->first_column,
llocp->last_line, llocp->last_column, error
);
}
%}
%token tokGame
%token tokFunction
%token tokSituation
%token tokWhile
%token tokFor
%token tokEach
%token tokIn
%token tokIf
%token tokThen
%token tokElse
%token tokNot
%token tokCommand
%token tokPlayer
%token <sValue> tokSituationName
%token <sValue> tokIdent
%token tokGoto
%token <sValue> tokString
%token <iValue> tokInt
%token <dValue> tokFloat
%token <bValue> tokBool
%token tokNull
%destructor { delete $$; printf("string deleted.\n"); } tokString tokSituationName
%token eos 0 "end of stream"
%right '='
%left tokIn
%left '(' ')' '[' ']'
%left '*' '/'
%left '-' '+'
%%
input: gameDecl bodyDecl
;
gameDecl: tokGame '{' gameExprList '}'
;
gameExprList:
| gameExprList cmpltGameExpr
;
cmpltGameExpr: gameExpr ';'
| commandDecl
;
gameExpr: tokIdent '=' expr
;
bodyDecl:
| bodyDecl situation
| bodyDecl function
;
situation: tokSituation tokSituationName { printf("Read situtaion: %s\n", (*$2).getStr() ); } '{' cmpltExprList '}'
;
function: tokFunction tokIdent '(' ')' '{' '}'
;
cmpltExprList:
| cmpltExprList cmpltExpr
;
cmpltExpr: expr ';'
| tokGoto '(' expr ')' ';'
| tokIf expr tokThen '{' cmpltExprList '}' ifnext
;
ifnext:
| tokElse '{' cmpltExprList '}'
| tokElse tokIf '{' cmpltExprList '}' ifnext
;
expr: tokInt
| tokFloat
| tokString
| tokBool
| tokNull
| tokIdent
| tokSituationName
| tokSituation '.' tokIdent
| tokPlayer '.' tokIdent
| expr '+' expr
| expr '-' expr
| expr '/' expr
| expr '*' expr
| expr tokIn expr
| '(' expr ')'
| expr '[' expr ']'
| '[' ']'
| '[' listValues ']'
| '{' '}'
| '{' dictValues '}'
/* | tokNot expr */
;
listValues: expr
| listValues ',' expr
;
dictValues: expr ':' expr
| dictValues ',' expr ':' expr
;
commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}'
;
commandParamList:
| commandParamList tokString
| commandParamList tokIdent
;
%%
void yylex_init( yyscan_t * );
void yylex_destroy( yyscan_t );
int main()
{
yyscan_t scanner;
yylex_init ( &scanner );
yyparse( scanner );
yylex_destroy ( scanner );
return 0;
}
|